package com.indexingsystem.boss.utils;

import com.indexingsystem.boss.Vo.ExportTableWithOrderResVo;
import com.indexingsystem.boss.Vo.ExportWordsWithOrderResVo;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class ThemeWordsExcelUtil extends ExcelUtil {

    private String[] heads = {"主题词表顺序", "主题词表ID", "主题词表", "顺序", "主题词ID", "编号", "主题词", "状态", "描述"};

    public void ExportWbToFile(HSSFWorkbook wb, String filePath) throws IOException {
        SimpleDateFormat df1 = new SimpleDateFormat("yyyyMMddHHmmss");
        SimpleDateFormat df2 = new SimpleDateFormat("yyyy-MM-dd");
        String dateFilePathStr = df2.format(new Date());
        String dateFileStr = df1.format(new Date());
        String exportPath = null;
        if (filePath.endsWith("\\")) {
            exportPath = filePath + dateFilePathStr;
        } else {
            exportPath = filePath + "\\" + dateFilePathStr;
        }
        File file = new File(exportPath);
        if (!file.exists()) {
            file.mkdirs();
        }

        FileOutputStream out = new FileOutputStream(exportPath + "\\" + "ThemeWords" + dateFileStr + ".xls");
        wb.write(out);
        out.flush();
        out.close();
    }

    public HSSFWorkbook doExportXLS(List<ExportTableWithOrderResVo> dataList, String sheetname) {
        HSSFWorkbook wb = new HSSFWorkbook();
        if (sheetname == null || sheetname == "") {
            sheetname = "sheet";
        }
        HSSFSheet sheet = wb.createSheet(sheetname);
        createXLSHeader(sheet);
        int row = 1;
        if (dataList != null && dataList.size() > 0) {
            for (ExportTableWithOrderResVo table : dataList) {
                row = addThemeTable(wb, sheet, row, table);
            }
        }

        return wb;
    }


    public void createXLSHeader(HSSFSheet sheet) {
        for (int i = 0; i < heads.length; i++) {
            setStringValue(sheet, (short) 0, (short) i, heads[i]);
        }
    }

    /**
     * 添加主题词表格
     *
     * @param wb
     * @param sheet
     * @param row
     * @param data
     * @return
     */
    public int addThemeTable(HSSFWorkbook wb, HSSFSheet sheet, int row, ExportTableWithOrderResVo data) {

        doSetCell(wb, sheet, (short) row, (short) 0, data.getOrder());
        doSetCell(wb, sheet, (short) row, (short) 1, data.getThemeTableId() );
        doSetCell(wb, sheet, (short) row, (short) 2, data.getThemeTableName());
        List<ExportWordsWithOrderResVo> words = data.getWords();
        if (words != null && words.size() > 0) {
            for (ExportWordsWithOrderResVo r : words) {
                row = addThemeWord(wb, sheet, row, r);
            }
        } else {
            row++;
        }
        List<ExportTableWithOrderResVo> tables = data.getTables();
        if (tables != null && tables.size() > 0) {
            for (ExportTableWithOrderResVo t : tables) {
                row = addThemeTable(wb, sheet, row, t);
            }
        }
        return row;
    }

    /**
     * 添加主题词到excel
     *
     * @param wb
     * @param sheet
     * @param row
     * @param data
     * @return
     */
    public int addThemeWord(HSSFWorkbook wb, HSSFSheet sheet, int row, ExportWordsWithOrderResVo data) {

        doSetCell(wb, sheet, (short) row, (short) 3, data.getOrder());
        doSetCell(wb, sheet, (short) row, (short) 4, data.getThemeWordsId());
        doSetCell(wb, sheet, (short) row, (short) 5, data.getThemeWordsCode());
        doSetCell(wb, sheet, (short) row, (short) 6, data.getThemeWordsName());
        if (data.getVerify() != null && data.getVerify() != "") {
            if (data.getVerify().equals("Y")) {
                doSetCell(wb, sheet, (short) row, (short) 7, "已核");
            } else {
                doSetCell(wb, sheet, (short) row, (short) 7, "未核");
            }
        }
        doSetCell(wb, sheet, (short) row, (short) 8, data.getDescription());
        List<ExportWordsWithOrderResVo> words = data.getWords();
        if (words != null && words.size() > 0) {
            row++;
            for (ExportWordsWithOrderResVo r : words) {
                row = addThemeWord(wb, sheet, row, r);
            }
        } else {
            row++;
        }

        return row;
    }
}
